package com.java.zhuiyun.api.live.service.impl;

import com.java.zhuiyun.api.live.controller.LiveController;
import com.java.zhuiyun.api.live.entity.Live;
import com.java.zhuiyun.api.live.mapper.LiveMapper;
import com.java.zhuiyun.api.live.service.LiveService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.OssTemplate;
import com.java.zhuiyun.util.pageUtil.MybatisPageHelper;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import com.java.zhuiyun.util.pageUtil.PageResult;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @ClassName LiveServiceImpl
 * @Description TODO
 * @Author MJL
 * @Date 2023/8/21 9:35
 **/
@Service
@Slf4j
public class LiveServiceImpl implements LiveService {
    @Resource
    LiveMapper liveMapper;
    @Resource
    OssTemplate ossTemplate;

    /**
    * @Author MJL
    * @Description todo 查看所有直播间信息
    * @Date 2023/8/21 10:22
    * @Return  * @param null
    */
    @Override
    public PageResult queryLive(Live live, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        PageResult pageResult = MybatisPageHelper.getPageResult(liveMapper.queryLive(live));
        return pageResult;
    }

    /**
    * @Author MJL
    * @Description todo 将违规直播间封禁
    * @Date 2023/8/21 14:07
    * @Return  * @param null
    */
    @Override
    public CommonResult banned(Integer id) {
        Integer banned = liveMapper.banned(id);
        if (banned>=1){
            return CommonResult.ok("禁用成功");
        }
            return CommonResult.error("禁用失败");
    }

    /**
    * @Author MJL
    * @Description todo 将直播间解封
    * @Date 2023/8/21 14:13
    * @Return  * @param null
    */
    @Override
    public CommonResult enable(Integer id) {
        Integer enable = liveMapper.enable(id);
        if (enable>=1){
            return CommonResult.ok("禁用成功");
        }
        return CommonResult.error("禁用失败");
    }

    @Override
    public CommonResult saveLive(Live live, MultipartRequest multipartRequest,MultipartFile file) throws IOException {
        String upload = ossTemplate.upload(file.getOriginalFilename(), file.getInputStream());
        log.info("数据" + upload);
        // 获取直播间封面
        String coverId = upload;
        String streamName = DigestUtils.md5DigestAsHex(live.getTeacherId().toString().getBytes());
        Map<Object, Object> pushUrlAndPlayUrl = LiveController.createPushUrlAndPlayUrl(streamName);
        // 推流地址
        Object streamKey = pushUrlAndPlayUrl.get("streamKey");
        // 播流地址
        Object playUrl = pushUrlAndPlayUrl.get("playUrl");
        // 服务器地址
        Object pushUrlHead = pushUrlAndPlayUrl.get("pushUrlHead");
        live.setLivePlayUrl(playUrl.toString());
        live.setLiveServerUrl(pushUrlHead.toString());
        live.setStreamKey(streamKey.toString());
        live.setCoverId(coverId);
        live.setLiveStatus(0);
        live.setDictId(6);
        live.setLiveRoomStatus(0);
        live.setDelFlag(0);
        // 随机设置直播间房间号
        Integer roomNum = checkRommNum();
        live.setRoomNum(roomNum);
        live.setCreateTime(new Date());
        int insert = liveMapper.insertSelective(live);
        System.out.println("live:::"+live);
        if (insert > 0) {
            return CommonResult.ok(insert);
        } else {
            return CommonResult.error("401");
        }
    }

    /**
    * @Author MJL
    * @Description todo 根据教师id查询直播信息
    * @Date 2023/8/23 14:37
    * @Return  * @param null
    */
    @Override
    public CommonResult getLiveByUserId(Integer teacherId) {
        Live live = liveMapper.getLiveByUserId(teacherId);
        return CommonResult.ok(live);
    }

    /**
    * @Author MJL
    * @Description todo 关闭直播
    * @Date 2023/8/23 15:08
    * @Return  * @param null
    */
    @Override
    public CommonResult closeLive(Live live) {
        Date date = new Date();
        live.setUpdateTime(date);
        live.setLiveStatus(1);
        int i = liveMapper.closeLive(live);
        if (i>0){
            return CommonResult.ok(i);
        } else {
            return CommonResult.error("401");
        }
    }

    /**
    * @Author MJL
    * @Description todo 检验当前主播是否正在直播
    * @Date 2023/8/23 15:29
    * @Return  * @param null
    */
    @Override
    public CommonResult checkLiveByUserId(Integer teacherId) {
        Integer integer = liveMapper.checkLiveByUserId(teacherId);
        if (integer > 0) {
            return CommonResult.error("401");
        } else {
            return CommonResult.ok(integer);
        }
    }

    @Override
    public CommonResult getLiveById(Integer id) {
        Live live = liveMapper.getLiveById(id);
        return CommonResult.ok(live);
    }

    /**
    * @Author MJL
    * @Description todo 查询出正在直播的直播间
    * @Date 2023/8/24 19:27
    * @Return  * @param null
    */
    @Override
    public CommonResult listLive() {
        Example example = new Example(Live.class);
        example.createCriteria().andEqualTo("liveStatus", 0);
        List<Live> lives = liveMapper.selectByExample(example);
        return CommonResult.ok(lives);
    }

    /**
    * @Author MJL
    * @Description todo 生成房间号
    * @Date 2023/8/23 10:54
    * @Return  * @param null
    */
    public Integer checkRommNum(){
        int roomNum = (int) (((Math.random() * 9) + 1) * 100000);
        // 校验直播间 房间号是否重复
        Integer roomNumCount = liveMapper.checkRoomNum(roomNum);
        if (roomNumCount > 0) {
            checkRommNum();
        }
        return roomNum;
    }
}
